package com.robgfx.coalex.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class LaunchDialog {
	
	private Display display;
	private Shell shell;
	private Color bgColor;
	private Composite activeView;


	public LaunchDialog() {
		buildGUI();
	}
	
	
	private void buildGUI() {
		display = Display.getDefault();
		
		display.setAppName("CoaLex");
		display.addFilter(SWT.KeyDown, new Listener() {

			public void handleEvent(Event event) {
				if(event.keyCode == SWT.F6) {
					toggleFullScreen();
				}
			}
			
		});
		
		Shell loadingShell = new Shell(display);
		loadingShell.setSize(100, 100);
		loadingShell.setLayout(new FillLayout(SWT.HORIZONTAL));
		Label l = new Label(loadingShell, SWT.None);
		l.setText("Loading CoaLex...");
		loadingShell.pack();
		loadingShell.open();
		
		loadingShell.update();
		
		shell = new Shell (display);
		shell.setLayout (new FillLayout());
		shell.setText("CoaLex");
		
		
		this.bgColor = display.getSystemColor(SWT.COLOR_WHITE);
//		final Image bgImg = new Image(display,"/media/mmc1/coalex.png");
		final Image bgImg = new Image(display, LaunchDialog.class.getResourceAsStream("coalex.png"));
		shell.setBackgroundImage(bgImg);
//		shell.setBackground(bgColor);
		shell.setBackgroundMode(SWT.INHERIT_FORCE);
//        final Image cardsImage = null;
		
		buildMenuView();
		
		shell.setSize(800,480);
		loadingShell.dispose();
		shell.open ();
	}
	
	protected void buildMenuView() {
		Composite menuView = new Composite(shell, SWT.NONE);
		this.activeView = menuView;
		menuView.setLayout(new GridLayout());
		final Image cardsImage = new Image(display,
				LaunchDialog.class.getResourceAsStream("notecards256.png"));

//		final Image dictionaryImage = null;
		final org.eclipse.swt.graphics.Image dictionaryImage = new Image(display,
				LaunchDialog.class.getResourceAsStream("book256.png"));

//		shell.setFullScreen(true);
		Composite mainButtonPanel = new Composite(menuView, SWT.NONE);
		GridData gridData = new GridData();
		gridData.grabExcessVerticalSpace = true;
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.CENTER;
		gridData.verticalAlignment = GridData.CENTER;
		mainButtonPanel.setLayoutData(gridData);
		mainButtonPanel.setLayout(new RowLayout(SWT.HORIZONTAL));
		TranslucentButton dictButton = new TranslucentButton(mainButtonPanel, "Dictionary", dictionaryImage);
		dictButton.addMouseListener(new MouseListener() {

			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {
				// TODO: handle mouse up, build dictionary view
				buildDictionaryView();
			}
			
		});
		TranslucentButton cardsButton = new TranslucentButton(mainButtonPanel,"Practice", cardsImage);
		cardsButton.addMouseListener(new MouseListener() {
			public void mouseDoubleClick(MouseEvent e) {
			}

			public void mouseDown(MouseEvent e) {
			}

			public void mouseUp(MouseEvent e) {
				// TODO: build flash cards view
				buildPracticeView();
			}
			
		});
	}

	protected void buildPracticeView() {
		if(this.activeView != null && !this.activeView.isDisposed())
			this.activeView.dispose();

		Composite practiceView = new Composite(shell, SWT.NONE);
		practiceView.setLayout(new FillLayout());
		Label label = new Label(practiceView, SWT.NONE);
		label.setText("TODO: Add Practice view");
		this.activeView = practiceView;
		shell.layout();
	}


	protected void buildDictionaryView() {
		if(this.activeView != null && !this.activeView.isDisposed())
			this.activeView.dispose();
		
		Composite dictionaryView = new DictionaryView(shell);
		this.activeView = dictionaryView;
		shell.layout();
	}


	protected void toggleFullScreen() {
		if(getShell() != null) {
			getShell().setFullScreen(!getShell().getFullScreen());
		}
	}


	public Display getDisplay() {
		return display;
	}


	public Shell getShell() {
		return shell;
	}

	public static void main(String[] args) {
		LaunchDialog launchDialog = new LaunchDialog();
		
		while (!launchDialog.getShell().isDisposed ()) {
			if (!launchDialog.getDisplay().readAndDispatch ())
				launchDialog.getDisplay().sleep ();
		}
		launchDialog.dispose();
	}


	private void dispose() {
		this.bgColor.dispose();
		this.getDisplay().dispose();
	}
}
